/**
 * Copyright &copy; 2013-2015 山东易科德软件有限公司 All rights reserved.
 */
package cn.net.ecode.common.utils;

import java.security.SecureRandom;
import java.util.UUID;

/**
 * 封装各种生成唯一性ID算法的工具类.
 * @author ThinkGem
 * @version 2014-8-19
 */
public class IdGenerate {

	private static SecureRandom random = new SecureRandom();
	private static IdWorker idWorker = new IdWorker(-1, -1);
	
	/**
	 * 生成UUID, 中间无-分割.
	 */
	public static String uuid() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
	
	/**
	 * 生成UUID，数字版本
	 */
	public static String uuidLong() {
		return String.valueOf(idWorker.nextId());
	}
	
	/**
	 * 使用SecureRandom随机生成Long. 
	 */
	public static long randomLong() {
		return Math.abs(random.nextLong());
	}

	/**
	 * 基于Base62编码的SecureRandom随机生成bytes.
	 */
	public static String randomBase62(int length) {
		byte[] randomBytes = new byte[length];
		random.nextBytes(randomBytes);
		return Encodes.encodeBase62(randomBytes);
	}
	
	/**
	 * 获取新唯一编号
	 */
	public static String nextId() {
		return uuidLong();
	}
	
	public static void main(String[] args) {
//		Set<String> set = Sets.newHashSet();
//		try{
//			for (int i=0; i<1000000; i++){
//				String id = String.valueOf(uuidLong());
//				if (set.contains(id)){
//					throw new Exception(id + " exists");
//				}
//				set.add(id);
//				System.out.println(id + " " + id.length());
//			}
//		}catch (Exception e) {
//			e.printStackTrace();
//		}
		System.out.println(uuid());
		System.out.println(uuidLong());
	}

}
